from django.contrib.auth.models import User
from django.db import models


# Create your models here.
class Label(models.Model):
    name = models.CharField(max_length=30, verbose_name='标签名', unique=True)
    user = models.ForeignKey(User, on_delete=models.CASCADE)

    class Meta:
        db_table = 'label'
        verbose_name = '标签'
        verbose_name_plural = verbose_name


class Topic(models.Model):
    TYPE_CHOICES = (
        (0, '单选题'),
        (1, '多选题'),
        (2, '判断题'),
        (3, '填空题'),
        (4, '问答题')
    )
    subject = models.TextField(verbose_name='题目内容')
    type = models.IntegerField(verbose_name='类型', choices=TYPE_CHOICES)
    score = models.FloatField(verbose_name='分值')
    description = models.TextField(verbose_name='题目描述', null=True, blank=True)
    answer = models.TextField(verbose_name='答案')
    create_time = models.DateTimeField(auto_now_add=True, verbose_name='创建时间')
    update_time = models.DateTimeField(auto_now=True, verbose_name='更新时间')
    is_delete = models.BooleanField(default=False, verbose_name='逻辑删除')

    label = models.ForeignKey(Label, on_delete=models.CASCADE)
    user = models.ForeignKey(User, on_delete=models.CASCADE)

    class Meta:
        ordering = ['id']
        db_table = 'topic'
        verbose_name = '题目'
        verbose_name_plural = verbose_name

    def delete(self, using=None, keep_parents=False):
        self.is_delete = True
        self.save()


class Accessory(models.Model):
    TYPE_CHOICES = (
        (0, '图片'),
        (1, '数据文件')
    )
    name = models.CharField(max_length=30, verbose_name='附件名')
    path = models.TextField(verbose_name='存储地址')
    type = models.IntegerField(verbose_name='附件类型', choices=TYPE_CHOICES)
    is_delete = models.BooleanField(default=False, verbose_name='逻辑删除')

    topic = models.ForeignKey(Topic, on_delete=models.CASCADE)

    class Meta:
        db_table = 'accessory'
        verbose_name = '附件'
        verbose_name_plural = verbose_name

    def delete(self, using=None, keep_parents=False):
        self.is_delete = True
        self.save()


class TopicRecord(models.Model):
    topic = models.TextField(verbose_name='答题id')
    answer = models.TextField(verbose_name='提交答案', null=True, blank=True)
    score = models.FloatField(verbose_name='成绩')
    accessory = models.TextField(verbose_name='附件地址', null=True, blank=True)

    user = models.ForeignKey(User, on_delete=models.CASCADE)

    class Meta:
        db_table = 'topic_record'
        verbose_name = '答题记录'
        verbose_name_plural = verbose_name

